import {Constant} from '../Constant.js';
import {ElementUtils} from '../utils/ElementUtils.js';
import {DragElements} from './DragElements.js';
import {TableAttribute} from './TableAttribute.js';


class AppElementInfo {
  constructor(appId, appName, appFields, elementListType = Constant.ELEMENT_LIST_TYPE_DRAG) {
    this.appName = appName;
    this.appId = appId;
    this.appFields = appFields;
    this.elementListType = elementListType;
  }

  generateTableElements() {
    const userCode = kintone.getLoginUser().code;
    const deprecatedKeys = [];
    const diagramElements = new DragElements(this.appId);
    const fields = this.appFields;
    diagramElements.appName = this.appName;
    diagramElements.addElement(ElementUtils.generateElement(this.appId, new TableAttribute(`${this.appName} (appId:${this.appId})`),
      Constant.NOT_NEED_TO_BIND_POINT, Constant.STATUS_TITLE_ELEMENT));
    const keys = Object.keys(fields);


    keys.forEach(key => {
      if (fields[key].type === Constant.APP_DATA_TYPE_ID || fields[key].type === Constant.APP_DATA_TYPE_RECORD_NUMBER
        || fields[key].type === Constant.APP_DATA_TYPE_CREATOR || fields[key].type === Constant.APP_DATA_TYPE_MODIFIER
        || fields[key].type === Constant.APP_DATA_TYPE_CREATED_TIME || fields[key].type === Constant.APP_DATA_TYPE_UPDATED_TIME
        || fields[key].type === Constant.APP_DATA_TYPE_STATUS || fields[key].type === Constant.APP_DATA_TYPE_STATUS_ASSIGNEE
        || fields[key].type === Constant.APP_DATA_TYPE_CATEGORY || fields[key].type === Constant.APP_DATA_TYPE_REVISON) {
        deprecatedKeys.push(key);
      }
    });
    this.deleteKeysFromArray(keys, deprecatedKeys);

    keys.forEach((key) =>{
      const elementId = `${key}_${this.appId}`;
      const recordValue = this.appFields[key];
      if (recordValue.type === Constant.APP_DATA_TYPE_SUBTABLE && recordValue.fields) {
        diagramElements.addElement(ElementUtils.generateElement(elementId, new TableAttribute(recordValue.label, key),
          Constant.NOT_NEED_TO_BIND_POINT));
        const tableKeys = Object.keys(recordValue.fields);
        (tableKeys).forEach((tableKey, index) =>{
          diagramElements.addElement(ElementUtils.generateElement(`${elementId}_${tableKey}`,
            new TableAttribute(recordValue.fields[tableKey].label, tableKey)
            , Constant.NEED_TO_BIND_POINT, Constant.STATUS_SUBTABLE_ELEMENT, index === (tableKeys.length) - 1));
        });
      } else {
        diagramElements.addElement(ElementUtils.generateElement(elementId, new TableAttribute(recordValue.label, key)));
      }
    });
    diagramElements.keys = keys;
    return diagramElements;
  }
  deleteKeysFromArray(keys, deprecatedKeys) {
    deprecatedKeys.forEach(deprecatedKey =>{
      this.deleteTargetFromArray(keys, deprecatedKey);
    });
  }
  deleteTargetFromArray(keys, target) {
    const index = keys.indexOf(target);
    if (index > -1) {
      keys.splice(index, 1);
    }
  }
}
export {
  AppElementInfo
};